package com.czy.owner.ui.business;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.czy.owner.R;
import com.czy.owner.adapter.BaseRecyclerAdapter;
import com.czy.owner.adapter.ScopeBusinessAdapter;
import com.czy.owner.adapter.ScopeBusinessSelectAdapter;
import com.czy.owner.api.ScopeBusinessApi;
import com.czy.owner.callback.RecycleViewDivider;
import com.czy.owner.db.UserHelper;
import com.czy.owner.entity.ScopeBusinessModel;
import com.czy.owner.entity.WebViewRequestParam;
import com.czy.owner.global.Constants;
import com.czy.owner.net.api.BaseResultEntity;
import com.czy.owner.net.http.HttpManager;
import com.czy.owner.net.listener.HttpOnNextListener;
import com.czy.owner.ui.BaseActivity;
import com.czy.owner.ui.order.ProjectDetailsWebviewActivity;
import com.czy.owner.utils.PhoneUtils;
import com.czy.owner.utils.SharedPreferencesUtils;
import com.easyrecycleview.EasyRecyclerView;
import com.easyrecycleview.adapter.RecyclerArrayAdapter;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

import static com.czy.owner.entity.WebViewRequestParam.REQUEST_POST;

/**
 * Created by wangyang on 2016/7/7 0007.
 * 业务范围
 */
public class ScopeBusinessActivity extends BaseActivity {
    @BindView(R.id.easy_recycleview)
    EasyRecyclerView recyclerView;
    private ScopeBusinessAdapter adapter;
    private Dialog mTypeDialog;
    private ScopeBusinessSelectAdapter scopeBusinessSelectAdapter;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_scope_business;
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initViews() {
        getToolbarTitle().setText(R.string.store_scope_business);
        recyclerView.setLayoutManager(new GridLayoutManager(this, 3));
        recyclerView.addItemDecoration(new RecycleViewDivider(this, LinearLayoutManager.VERTICAL, R.drawable.shape_scopebusniess_divider_decoration));
        recyclerView.setAdapterWithProgress(adapter = new ScopeBusinessAdapter(this));
        adapter.setError(R.layout.view_error_net, new RecyclerArrayAdapter.OnErrorListener() {
            @Override
            public void onErrorShow() {
                adapter.resumeMore();

            }

            @Override
            public void onErrorClick() {
                adapter.resumeMore();

            }
        });
        adapter.setOnItemClickListener(new RecyclerArrayAdapter.OnItemClickListener<ScopeBusinessModel>() {
            @Override
            public void onItemClick(View view, int position, ScopeBusinessModel model) {
                if (model.getSubs() == null || model.getSubs().size() == 0) {
                    if (PhoneUtils.isNetworkConnected(ScopeBusinessActivity.this)) {
                        setIntentDetails(model.getStoreServiceId() + "");
                    }
                } else {
                    TextView tv_name = (TextView) view.findViewById(R.id.tv_name);
                    tv_name.setTextColor(ScopeBusinessActivity.this.getResources().getColor(R.color.appThemeColor));
                    showTypeDialog(model);
                }


            }
        });

        getDataList();
    }

    private void showTypeDialog(final ScopeBusinessModel scopeBusinessModel) {
        WindowManager windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
        Display mDisplay = windowManager.getDefaultDisplay();
        mTypeDialog = new Dialog(this, R.style.AlertDialogStyle);
        View dialogView = LayoutInflater.from(this).inflate(R.layout.dialog_scope_business, null);


        RecyclerView rvType = (RecyclerView) dialogView.findViewById(R.id.rv_archive_add_type);
        RecyclerView.LayoutManager layoutManager = new GridLayoutManager(this, 3);
        rvType.setLayoutManager(layoutManager);

        scopeBusinessSelectAdapter = new ScopeBusinessSelectAdapter(this, scopeBusinessModel.getSubs());
        rvType.setAdapter(scopeBusinessSelectAdapter);
        if (scopeBusinessModel.getSubs().size() > 6) {
            LinearLayout.LayoutParams linearParams = (LinearLayout.LayoutParams) rvType.getLayoutParams(); //取控件textView当前的布局参数 linearParams.height = 20;// 控件的高强制设成20
            linearParams.height = PhoneUtils.dip2px(this, 38 * 3);
            rvType.setLayoutParams(linearParams); //使设置好的布局参数应用到控件
        }
        mTypeDialog.setContentView(dialogView);
        mTypeDialog.setCancelable(true);
        mTypeDialog.setCanceledOnTouchOutside(true);
        mTypeDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                adapter.notifyDataSetChanged();
            }
        });
        scopeBusinessSelectAdapter.setOnItemClickListener(new BaseRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position, Object o, View view) {
                mTypeDialog.dismiss();
                if (PhoneUtils.isNetworkConnected(ScopeBusinessActivity.this)) {
                    setIntentDetails(scopeBusinessModel.getSubs().get(position).getStoreServiceId() + "");

                }
            }
        });
        Window dialogWindow = mTypeDialog.getWindow();
        WindowManager.LayoutParams p = dialogWindow.getAttributes(); // 获取对话框当前的参数值
        p.width = (int) (mDisplay.getWidth() * 0.86);
        dialogWindow.setAttributes(p);
        mTypeDialog.show();


    }

    public void setIntentDetails(String storeServiceId) {
        WebViewRequestParam webViewRequestParam = new WebViewRequestParam(Constants.APP_API_BASE + "/own/order/serviceInfo");
        webViewRequestParam.setRequestMethod(REQUEST_POST);
        webViewRequestParam.setParams("session=" + UserHelper.getInstance().getSessionInfoModel(
                this).getSession() + "&storeId=" + (SharedPreferencesUtils.getParam(this, UserHelper.getInstance().getUserAccount() + Constants.SHARED_PREFERENCES_KEY_BINDSTOREID, 0) + "") + "&storeServiceId="
                + storeServiceId);
        webViewRequestParam.setTitle("服务详情");
        Intent intent = new Intent(this, ProjectDetailsWebviewActivity.class);
        intent.putExtra(Constants.TRANSMISSION_CONTENT_KEY, webViewRequestParam);
        startActivity(intent);
    }

    @Override
    protected void updateViews() {

    }


    private void getDataList() {
        ScopeBusinessApi scopeBusinessApi = new ScopeBusinessApi(new HttpOnNextListener<List<ScopeBusinessModel>>() {
            @Override
            public void onNext(List<ScopeBusinessModel> list) {
                setDataList(list);
            }

            @Override
            public void onCacheNext(String cache) {
                super.onCacheNext(cache);
                Gson gson = new Gson();
                Type type = new TypeToken<BaseResultEntity<List<ScopeBusinessModel>>>() {
                }.getType();
                BaseResultEntity<List<ScopeBusinessModel>> cacheList = gson.fromJson(cache, type);
                setDataList(cacheList.getData());
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                PhoneUtils.ShowToastMessage(ScopeBusinessActivity.this, e.getMessage());
            }
        }, this);
        scopeBusinessApi.setSession(UserHelper.getInstance().getSessionInfoModel(ScopeBusinessActivity.this).getSession());
        scopeBusinessApi.setStoreId(SharedPreferencesUtils.getParam(ScopeBusinessActivity.this, UserHelper.getInstance().getUserAccount() + Constants.SHARED_PREFERENCES_KEY_BINDSTOREID, 0) + "");
        HttpManager httpManager = HttpManager.getInstance();
        httpManager.doHttpDeal(scopeBusinessApi);

    }

    private void setDataList(List<ScopeBusinessModel> list) {
        adapter.addAll(list);
        if (adapter.getAllData().size() == 0) {
            recyclerView.setEmptyView(R.layout.view_empty_common);
            recyclerView.showEmpty();
        }
    }


}
